// pages/authorized/index.js
var util = require('../../utils/util.js');
var user = require('../../services/user.js');
var api = require('../../config/api.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasUserInfo:true,
        hasMobile:false,
        phone:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    gotoIndex: function () {
        wx.reLaunch({
            url: '/pages/index/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        console.log('authorized onready');
    },
    getPhoneNumber: function(e){
        let params = {
            encryptedData:e.detail.encryptedData,
            iv:e.detail.iv
        }
        let _this = this;
        util.request(api.AuthLoginGetmobile,params,'POST').then(res=>{
            if(res.code==200){
                _this.setData({
                    phone:res.message,
                    hasUserInfo:false,
                    hasMobile:true
                })
            }
            
        }).catch(err=>{
            wx.clearStorageSync();
            console.log(222,err)
        })
    },
    getInfo: function () {
        let that = this;
        wx.showModal({
            title: '温馨提示',
            content: '正在请求您的个人信息',
            success(res) {
              if (res.confirm) {
                wx.getUserProfile({
                desc: "获取你的昵称、头像、地区及性别",
                success: res => {
                  
                  let wxUserInfo = {
                      ...res.userInfo,
                      phone:that.data.phone,
                  }

                  console.log(123,wxUserInfo)
                  util.wxLogin(wxUserInfo).then(res => {
                    app.globalData.userInfo = {
                        ...app.globalData.userInfo,
                        ...res.result
                    }
                    let thisUserInfo = {
                        ...app.globalData.userInfo,
                        nikeName:app.globalData.userInfo.nikeName,  
                        phone:that.data.phone,
                    }
                    wx.setStorageSync('userInfo', thisUserInfo)

                    wx.navigateBack();
                    wx.removeStorageSync('isPost401');
                
                    
                }).catch((err) => {
                        wx.showToast({
                        title: '授权失败，可能是网络问题',
                        icon:'none'
                    })
                    console.log(err)
                });

                },
                fail: res => {
                     //拒绝授权
                  that.showErrorModal('您拒绝了请求');
                  return;
                }
              })} else if (res.cancel) {
                //拒绝授权 showErrorModal是自定义的提示
                that.showErrorModal('您拒绝了请求');
                return;
              }
            }
          })



        

        
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this;
        //获取用户的登录信息
        user.checkLogin().then(res => {
            let userInfo = wx.getStorageSync('userInfo');
            if(userInfo.phone){
                app.globalData.userInfo = JSON.parse(userInfo);
                app.globalData.token = wx.getStorageSync('token');
                wx.navigateBack({
                    delta: 1
                })
            }else{
                that.setData({
                    hasMobile:false
                })
            }
            
        }).catch((e) => {
                //清空缓存
            wx.removeStorageSync('userInfo');
            that.setData({
                hasMobile: false
            })
            wx.setNavigationBarTitle({
                title: '用户授权'
            })
        });
    },

})